<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-12 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'info':
				$this->getInfo();
				break;
			case 'tips':
				$this->getTips();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}

	private function getList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$result = get_news_list($this->request);
		
		if(!check_array($result['result'])) $this->message('ne_01', STATUS_F);
		
		$list = array();
		foreach($result['result'] as $temp)
		{
			if($temp['is_show'])
			{
				$temp['desc'] = cut_string($temp['desc'], 140);
				$temp['publish_date'] = date('Y-m-d', $temp['publish_date']);
				
				$list[] = $temp;
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function getInfo()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? $this->request['id'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_news_info(array('id'=>$id));
		
		if(!isset($result['id']) || $result['id'] < 1)
		{
			$this->message('ne_01', STATUS_E);
		}
		
		$result['publish_date'] = date('Y-m-d', $result['publish_date']);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $result;
	}
	
	private function getTips()
	{
		$result = get_tips_list($this->request);
		$list = $result['result'];
		
		if(!check_array($list)) $this->message('ne_02', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['create_date'] = date('Y-m-d', $temp['create_date']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
}